import GameCfgData from "../../lib/GameCfgData";
import { TipManager } from "../../lib/TipManager";
import {ui} from "../../ui/layaMaxUI";
import ItemBaseData from "../data/ItemBaseData";
import Grid from "../Grid";
import Item from "../view/Item";

export default class EidtScene extends ui.EidtSceneUI{
    
    private mgrid:Grid;
    private itemLen:number=3;
    private id:number=0;
    private step:number=25;
    private mSelect:Item;
    private mIsMouseDown:boolean;
    private mStarScore1:number=3000;
    private mStarScore2:number=10000;
    private mStarScore3:number=40000;
    private mTaskType:number=0;
    private mListData:Array<ItemBaseData>=[];
    constructor(){
        super();
        this.list.renderHandler=new Laya.Handler(this,this.renderrr);
        this.list.mouseHandler=new Laya.Handler(this,this.mousehandler);
        this.on(Laya.Event.MOUSE_DOWN,this,this.downhandler);
        this.on(Laya.Event.MOUSE_UP,this,this.uphandler);
        this.on(Laya.Event.MOUSE_OUT,this,this.uphandler);
        this.on(Laya.Event.MOUSE_MOVE,this,this.movehandler)
        this.saveBtn.on(Laya.Event.CLICK,this,this.showSaveView,[true])
        this.saveView.getChildByName("okBtn").on(Laya.Event.CLICK,this,this.save,[true])
        this.saveView.getChildByName("closeBtn").on(Laya.Event.CLICK,this,this.showSaveView,[false]);
     
        this.mgrid=new Grid();
        this.mgrid.setItemParent(this.itemParent);
        this.mgrid.setR(300,true);
        this.drawLine();
    }

  

    private renderrr(_sp:Laya.Sprite,_index:number){
        let tdata = this.mListData[_index];
        _sp.getChildByName("icon")["skin"]=tdata.icon;
        let timg = _sp.getChildByName("img");
        timg["skin"]=this.getOverImg(tdata);
        timg["rotation"]=this.getAngle(tdata);
    }

    private getOverImg(_data:ItemBaseData){
        let tskin="";
        if(_data.isColorDiamand){
            tskin="items/zzz"+_data.colorIndex+"_1.png";
        }else if(_data.isDiamand){
            tskin="items/zzz6_"+_data.index+".png";
        }
        return tskin;
    }

    private getAngle(_data:ItemBaseData){
        let tangle = 0;
        if(_data.isDiamand){
            if(_data.index==3 || _data.index==5)tangle=180;
        }
        return tangle;
    }

    private mousehandler(e:Laya.Event,_index:number){
        if( this.saveView.visible)return;
        if(e.type==Laya.Event.MOUSE_DOWN){
            if(this.mSelect){
                this.mSelect.removeSelf();
                this.mSelect=null;
            }
            let tdata = this.list.array[_index].clone();
            this.mSelect=new Item(tdata,false);
          
          
            this.itemParent.addChild(this.mSelect);
            this.mIsMouseDown=true;
            this.setItemScrollViewEnable(false);
        }
        e.stopPropagation();
    }

    private downhandler(e:Laya.Event){
        if( this.saveView.visible)return;
        let tp=this.itemParent.globalToLocal(new Laya.Point(e.stageX,e.stageY));
        let titem:Item=this.mgrid.removeItemByPos(tp.x,tp.y);
        if(titem){
            // if(!this.canMove(titem)){
            //     TipManager.ins.showMsgAlert("这个不能移动");
            //     return;
            // }
            if(this.mSelect){
                this.mSelect.removeSelf();
                this.mSelect=null;
            }
            this.mSelect=titem;
           
            this.itemParent.addChild(this.mSelect);
            this.mIsMouseDown=true;
            this.setItemScrollViewEnable(false);
        }
        e.stopPropagation();
    }
    private uphandler(e:Laya.Event){
        this.setItemScrollViewEnable(true);
        if( this.saveView.visible)return;
        if(!this.mIsMouseDown)return;
        this.mIsMouseDown=false;
        if(this.mSelect){
            let tp=this.itemParent.globalToLocal(new Laya.Point(e.stageX,e.stageY));
            let trc = ItemBaseData.getRCByPos(tp.x,tp.y);
            this.mSelect.data.setRC(trc.row,trc.col);
            if(this.mgrid.isInLimit(trc.row,trc.col) && !this.mgrid.getItem(trc.row,trc.col)){
                this.mSelect.updatePos();
                this.mgrid.addItem(this.mSelect,false);
               
            }else{
                this.mSelect.removeSelf();
            }
            this.mSelect=null;
            
        }
    }

    private movehandler(e:Laya.Event){
        if( this.saveView.visible)return;
        if(this.mIsMouseDown){
            let tp=this.itemParent.globalToLocal(new Laya.Point(e.stageX,e.stageY));
            if(this.mSelect)this.mSelect.pos(tp.x,tp.y);
        }
        e.stopPropagation();
    }


    onEnable(){
      
     
     
       
        this.freshList();
    }

    private showSaveView(_show:boolean){
        this.saveView.visible=_show;
        if(_show){
            this.saveView.getChildByName("idTxt")["text"]=this.id+"";
            this.saveView.getChildByName("stepTxt")["text"]=this.step+"";
            this.saveView.getChildByName("s1Txt")["text"]=this.mStarScore1+"";
            this.saveView.getChildByName("s2Txt")["text"]=this.mStarScore2+"";
            this.saveView.getChildByName("s3Txt")["text"]=this.mStarScore3+"";
            this.saveView.getChildByName("taskType")["text"]=this.mTaskType+"";
        }
    }

    private freshList(){
        this.mListData = this.getListData();
        this.list.array= this.mListData;
       
    }

    private getListData(){
        let tarr = GameCfgData.getIns().getEditItems();
       return tarr;
    }

    // private canMove(_item:Item){
    //     if(_item.data.row==0){
    //         if(this.mgrid.getItem(_item.data.row,_item.data.col+1) || this.mgrid.getItem(_item.data.row,_item.data.col-1))return true;
    //     }else{
    //         let tupos = ItemBaseData.getUpPos(_item.data.row,_item.data.col);
    //         for(let i=0;i<tupos.length;i++){
    //             if(!this.mgrid.getItem(tupos[i].row,tupos[i].col))return false;
    //         }
    //     }
    //     return true;
    // }

  

    public save(){
        this.itemLen = this.mgrid.getPaoTypeNum();
        this.id=parseInt(this.saveView.getChildByName("idTxt")["text"]);
        this.step = parseInt(this.saveView.getChildByName("stepTxt")["text"]);
        this.mStarScore1=parseInt(this.saveView.getChildByName("s1Txt")["text"]);
        this.mStarScore2=parseInt(this.saveView.getChildByName("s2Txt")["text"]);
        this.mStarScore3=parseInt(this.saveView.getChildByName("s3Txt")["text"]);
        this.mTaskType=parseInt(this.saveView.getChildByName("taskType")["selectedIndex"]);
        let tdata={id:this.id,itemLen:this.itemLen,items:this.mgrid.getGridItemStr(),step:this.step,star1:this.mStarScore1,star2:this.mStarScore2,star3:this.mStarScore3,task:this.mTaskType}
        console.log(JSON.stringify(tdata));
        this.showSaveView(false);
        TipManager.ins.showMsgAlert("保存成功",this);
    }

    onDisable(){
      
        this.mgrid.clear();
        this.mgrid=null;
    }

    private drawLine(){
       
        this.itemParent.graphics.clear();
        for(let row=0;row<this.mgrid.row;row++){
            let tlen =  ItemBaseData.isSingleRow(row)?this.mgrid.col:this.mgrid.col-1;
            for(let col=0;col<tlen;col++){
                let tp = ItemBaseData.getPosByRC(row,col);
    
                this.itemParent.graphics.drawCircle(tp.x,tp.y,30,null,"#ffff00");
            }
        }
    }

    private isCanAdd(_data:ItemBaseData,_row:number,_col:number){
        if(this.mgrid.isInLimit(_row,_col))return false;
        if(this.mgrid.getItem(_row,_col))return false;
        let tarea = _data.getAreaRCs();
        if(tarea){
            let trow:number;
            let tcol:number;
            for(let i=0;i<tarea.length;i++){
                trow = tarea[i].row;
                tcol=tarea[i].col;
                if(this.mgrid.isInLimit(trow,tcol))return false;
                if(this.mgrid.getItem(trow,tcol))return false;
            }
        }
        return true;
    }

    private setItemScrollViewEnable(value:boolean){
       this.list.mouseEnabled=value;
       this.list.mouseThrough=value;
    }
}